<?php
class Index extends Action {
	private $limit = 15;
	/*
	 * (non-PHPdoc) @see Action::execute()
	 */
	public function execute($params) {
		check_login ( ADMIN_LOGIN_PAGE );
		$data ['navi'] = 'index';
		$start = rqst ( 'start', 1 ); // 分页
		$where = Model::where_build ( array ('title like' ,'page_mode','page_type'), $data, array ('title' => 'like' ) );
		$status = rqst ( 'status', 'draft' );
		$data ['status'] = $status;
		if ($status == 'deleted') {
			$where ['deleted'] = 1;
		} else {
			$where ['deleted'] = 0;
			$where ['status'] = $status;
		}
		
		$category = rqst ( 'category' );
		if ($category) {
			$where ['AND'] = array ('category' => $category,'OR' => array ('subcategory' => $category ) );
		}
		
		$pageModel = new WebPageModel ();
		$pageModel->alias ( "WP" );
		$pageModel->join ( "web_page_category as WPC", "WP.category = WPC.name" );
		$pageModel->join ( "web_page_category as SWPC", "WP.subcategory = SWPC.name" );
		$pageModel->join ( 'core_user AS CU', 'WP.create_uid = CU.uid' );
		$pageModel->join ( 'core_user AS PU', 'WP.update_uid = PU.uid' );
		$tag = irqst ( 'tag' ); // 页面标记
		if (! empty ( $tag )) { //
			$pageModel->join ( "web_page_tag WPT", "WP.page_id = WPT.page_id", Model::INNER );
			$data ['tag'] = $tag;
			$where ['WPT.tag_id'] = $tag;
		}
		
		$pageModel->where ( $where )->limit ( $this->limit, $start - 1 );
		
		$pageModel->sort ( array ('page_id','d' ) )->count ( true );
		$wcmt = new WebCommentModel();
		$cntSql =  $wcmt->alias('WMT')->where(array('WMT.page_id'=>'`WP`.`page_id`','WMT.status'=>'new'))->getSql('COUNT(WMT.comment_id)');
		$pageModel->subfield($cntSql,'comments');
		$items = $pageModel->retrieve ( 'WP.*,WPC.title as category_name,WPC.bind,WPC.home AS domain_home,WPC.cid as category_id,CU.name AS user_name,PU.name AS update_user_name,SWPC.title AS sub_category_name' );
		if ($items) {
			$data ['items'] = $items;
			$data ['countTotal'] = $items->countTotal;
		}
		$data ['curl'] = unkeepargs ( $this->url, 'start' );
		$data ['category'] = $category;
		$data ['cname'] = rqst ( 'cname' );
		
		$tagM = new WebTagModel ();
		$tags = $tagM->where ( array ('type' => 'flag' ) )->retrieve ( 'tag_id,tag' );
		if ($tags) {
			$data ['tag_options'] = $tags->toArray ( 'tag_id', 'tag', array ('0' => '属性' ) );
		} else {
			$data ['tag_options'] = array ('0' => '-标记-' );
		}
		$data ['limit'] = $this->limit;
		$cateModel = new WebPageCategory ();
		$data ['treeNodes'] = json_encode ( $cateModel->getTreeNodes ( 0 ) );
		
		$data ['draft_total'] = $pageModel->where ( array ('deleted' => 0,'status' => 'draft' ) )->count ();
		$data ['approving_total'] = $pageModel->where ( array ('deleted' => 0,'status' => 'approving' ) )->count ();
		$data ['unapproved_total'] = $pageModel->where ( array ('deleted' => 0,'status' => 'unapproved' ) )->count ();
		$data ['approved_total'] = $pageModel->where ( array ('deleted' => 0,'status' => 'approved' ) )->count ();
		$data ['published_total'] = $pageModel->where ( array ('deleted' => 0,'status' => 'published' ) )->count ();
		$data ['deleted_total'] = $pageModel->where ( array ('deleted' => 1 ) )->count ();
		$data ['page_tip_info'] = sess_del ( 'page_tip_info' );
		$data ['content_modes'] = apply_filter ( 'get_content_mode', array (''=>'选择模型') );
		$data ['page_types'] = apply_filter ( 'get_page_types', array ('' => '选择类型' ) );
		
		bind ( 'get_page_operation', array ($this,'get_page_operation' ), 1, 2 );
		bind ( 'get_page_bench_options', array ($this,'get_page_bench_options' ), 1, 2 );
		bind ( 'show_page_flags', array ($this,'show_page_flags' ), 1, 2 );
		return new SmartyView ( $data, 'vfs/Index' );
	}
	public function get_page_operation($options, $item) {
		$status = $item ['status'];
		if ($item ['deleted']) {
			$options .= '<a title="还原" class="reset tgre" href="./?Ctlr=SetPageStatus&del=0&pid=' . $item ['page_id'] . '"><i class="icon-share-alt"></i>还原</a>';
			$options .= '<a title="永久删除" onclick="return confirm(\'网页删除后不可恢复,你确定要删除该网页吗?\')" class="delete tred" href="./?Ctlr=DelPage&pid=' . $item ['page_id'] . '"><i class="icon-remove-sign"></i>永久删除</a>';
		} else {
			if ($status == 'draft') {
				$options .= '<a title="编辑" class="edit-page" data-content="' . $item ['page_id'] . '" id="edit-page-' . $item ['page_id'] . '" href="' . ADMIN_URL . 'vfs/?Ctlr=Page&pid=' . $item ['page_id'] . '"><i class="icon-edit"></i>编辑</a>';
				if (icando ( 'publish', 'page' )) {
					$options .= '<a title="发布" class="published tgre" href="./?Ctlr=SetPageStatus&s=published&pid=' . $item ['page_id'] . '"><i class="icon-check"></i>发布</a>';
				}
				$options .= '<a title="提交审核" class="approving" href="./?Ctlr=SetPageStatus&s=approving&pid=' . $item ['page_id'] . '"><i class="icon-filter"></i>提交审核</a>';
				$options .= '<a title="移至回收站" class="trash tred" href="./?Ctlr=SetPageStatus&del=1&pid=' . $item ['page_id'] . '"><i class="icon-trash"></i>移至回收站</a>';
			}
			if ($status == 'approving') {
				$options .= '<a title="批准" class="approved tgre" href="./?Ctlr=SetPageStatus&s=approved&pid=' . $item ['page_id'] . '"><i class="icon-thumbs-up"></i>批准</a>';
				$options .= '<a title="驳回" class="unapproved torg" href="./?Ctlr=SetPageStatus&s=unapproved&pid=' . $item ['page_id'] . '"><i class="icon-thumbs-down"></i>驳回</a>';
			}
			if ($status == 'approved') {				
				$options .= '<a title="编辑" class="edit-page" data-content="' . $item ['page_id'] . '" id="edit-page-' . $item ['page_id'] . '" href="' . ADMIN_URL . 'vfs/?Ctlr=Page&pid=' . $item ['page_id'] . '"><i class="icon-edit"></i>编辑</a>';
				$options .= '<a title="发布" class="published tgre" href="./?Ctlr=SetPageStatus&s=published&pid=' . $item ['page_id'] . '"><i class="icon-check"></i>发布</a>';
				$options .= '<a title="移至草稿箱" class="draft torg" href="./?Ctlr=SetPageStatus&s=draft&pid=' . $item ['page_id'] . '"><i class="icon-share"></i>移至草稿箱</a>';
				$options .= '<a title="移至回收站" class="trash tred" href="./?Ctlr=SetPageStatus&del=1&pid=' . $item ['page_id'] . '"><i class="icon-trash"></i>移至回收站</a>';
			}
			if ($status == 'unapproved') {
				$options .= '<a title="编辑" class="edit-page" data-content="' . $item ['page_id'] . '" id="edit-page-' . $item ['page_id'] . '" href="' . ADMIN_URL . 'vfs/?Ctlr=Page&pid=' . $item ['page_id'] . '"><i class="icon-edit"></i>编辑</a>';
				$options .= '<a title="移至草稿箱" class="draft torg" href="./?Ctlr=SetPageStatus&s=draft&pid=' . $item ['page_id'] . '"><i class="icon-share"></i>移至草稿箱</a>';
				$options .= '<a title="移至回收站" class="trash tred" href="./?Ctlr=SetPageStatus&del=1&pid=' . $item ['page_id'] . '"><i class="icon-trash"></i>移至回收站</a>';
			}
			if ($status == 'published') {
				$options .= '<a title="查看" href="' . the_safe_url($item).'" target="_blank"><i class="icon-eye-open"></i>查看</a>';
				$options .= '<a title="编辑" class="edit-page" data-content="' . $item ['page_id'] . '" id="edit-page-' . $item ['page_id'] . '" href="' . ADMIN_URL . 'vfs/?Ctlr=Page&pid=' . $item ['page_id'] . '"><i class="icon-edit"></i>编辑</a>';
				$options .= '<a title="移至草稿箱" class="draft torg" href="./?Ctlr=SetPageStatus&s=draft&pid=' . $item ['page_id'] . '"><i class="icon-share"></i>移至草稿箱</a>';
				$options .= '<a title="移至回收站" class="trash tred" href="./?Ctlr=SetPageStatus&del=1&pid=' . $item ['page_id'] . '"><i class="icon-trash"></i>移至回收站</a>';
			}
		}
		return $options;
	}
	// 页面批量操作
	public function get_page_bench_options($options, $status) {
		
		if ($status == 'deleted') {
			$options .= '<li><a title="还原" class="reset" href="./?Ctlr=SetPageStatus&del=0"><i class="icon-share-alt"></i>还原</a></li>';
			$options .= '<li><a title="永久删除" class="delete" href="./?Ctlr=DelPage"><i class="icon-remove-sign"></i>永久删除</a></li>';
		}
		if ($status == 'draft') {			
			$options .= '<li><a title="提交审核" class="approving" href="./?Ctlr=SetPageStatus&s=approving"><i class="icon-filter"></i>提交审核</a></li>';
			$options .= '<li><a title="移至回收站" class="trash" href="./?Ctlr=SetPageStatus&del=1"><i class="icon-trash"></i>移至回收站</a></li>';
		}
		if ($status == 'approving') {
			$options .= '<li><a title="批准" class="approved" href="./?Ctlr=SetPageStatus&s=approved"><i class="icon-thumbs-up"></i>批准</a></li>';
			$options .= '<li><a title="驳回" class="unapproved" href="./?Ctlr=SetPageStatus&s=unapproved"><i class="icon-thumbs-down"></i>驳回</a></li>';
		}
		if ($status == 'approved') {
			$options .= '<li><a title="发布" class="published" href="./?Ctlr=SetPageStatus&s=published"><i class="icon-check"></i>发布</a></li>';
			$options .= '<li><a title="移至草稿箱" class="draft" href="./?Ctlr=SetPageStatus&s=draft"><i class="icon-share"></i>移至草稿箱</a></li>';
			$options .= '<li><a title="移至回收站" class="trash" href="./?Ctlr=SetPageStatus&del=1"><i class="icon-trash"></i>移至回收站</a></li>';
		}
		if ($status == 'unapproved') {			
			$options .= '<li><a title="移至草稿箱" class="draft" href="./?Ctlr=SetPageStatus&s=draft"><i class="icon-share"></i>移至草稿箱</a></li>';
			$options .= '<li><a title="移至回收站" class="trash" href="./?Ctlr=SetPageStatus&del=1"><i class="icon-trash"></i>移至回收站</a></li>';
		}
		if ($status == 'published') {
			$options .= '<li><a title="移至草稿箱" class="draft" href="./?Ctlr=SetPageStatus&s=draft"><i class="icon-share"></i>移至草稿箱</a></li>';
			$options .= '<li><a title="移至回收站" class="trash" href="./?Ctlr=SetPageStatus&del=1"><i class="icon-trash"></i>移至回收站</a></li>';
		}
		
		return $options;
	}
	// 显示页面属性
	public function show_page_flags($flags, $item) {
		static $wptM = false;
		if (! $wptM) {
			$wptM = new WebPageTagModel ();
		}
		$fls = $wptM->getPageFlags ( $item ['page_id'] );
		if ($fls) {
			foreach ( $fls as $fname ) {
				$flags .= "<span class=\"label label-info\">{$fname['tag']}</span>&nbsp;";
			}
		}
		return $flags;
	}
}